package com.example.api.controller;

import com.example.api.aspect.SystemControllerLog;
import com.example.api.model.entity.Admin;
import com.example.api.model.enums.ActionTypeEnum;
import com.example.api.model.support.RespBean;
import com.example.api.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @ApiOperation("查询用户")
    @GetMapping("/")
    @SystemControllerLog(description = "查询用户", actionType = ActionTypeEnum.QUERY)
    public List<Admin> findAll(String name) {
        return adminService.findAll(name);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    @SystemControllerLog(description = "删除用户", actionType = ActionTypeEnum.DELETE)
    public RespBean delete(@PathVariable Integer id) {
        return adminService.delete(id);
    }

    @ApiOperation("添加用户")
    @PostMapping("/")
    @SystemControllerLog(description = "添加用户", actionType = ActionTypeEnum.CREATE)
    public RespBean save(@RequestBody Admin admin) throws Exception {
        return adminService.save(admin);
    }

    @ApiOperation("更新用户")
    @PutMapping("/")
    @SystemControllerLog(description = "更新用户", actionType = ActionTypeEnum.UPDATE)
    public RespBean update(@RequestBody Admin admin, Authentication authentication) {
        return adminService.update(admin, authentication);
    }

    @ApiOperation("更新用户角色")
    @PutMapping("/role")
    @SystemControllerLog(description = "更新用户角色", actionType = ActionTypeEnum.UPDATE)
    public RespBean updateAdminRole(Integer aid, Integer[] rids) {
        return adminService.updateAdminRole(aid, rids);
    }

    @ApiOperation("初始化密码")
    @PutMapping("/init-password")
    @SystemControllerLog(description = "初始化密码", actionType = ActionTypeEnum.UPDATE)
    public RespBean initPassword(Integer id) {
        return adminService.initPassword(id);
    }

}